﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TimeTracker.Comparers
{
    public class DateTimeComparer : IComparer<TreeNode>, System.Collections.IComparer
    {
        #region IComparer<RadTreeNode> Members

        int IComparer<TreeNode>.Compare(TreeNode x, TreeNode y)
        {
            if (x.Tag is DateTime && y.Tag is DateTime)
            {
                return ((DateTime)x.Tag).CompareTo(((DateTime)y.Tag));
            }

            if (x.Tag is IProject && y.Tag is IProject)
            {
                return ((IProject)x.Tag).Name.CompareTo(((IProject)y.Tag).Name);
            }

            if (x.Tag is ISession && y.Tag is ISession)
            {
                return ((ISession)x.Tag).StartTime.CompareTo(((ISession)y.Tag).StartTime);
            }

            if (x.Tag is INote && y.Tag is INote)
            {
                return ((INote)x.Tag).Time.CompareTo(((INote)y.Tag).Time);
            }

            return 0;
        }

        #endregion
        
        int System.Collections.IComparer.Compare(object x, object y)
        {
            if (x is TreeNode && y is TreeNode)
            {
                return ((IComparer<TreeNode>)this).Compare((TreeNode)x, (TreeNode)y);
            }

            return default(int);
        }
    }

    public class DateTimeComparerDescending : IComparer<TreeNode>, System.Collections.IComparer
    {
        #region IComparer<RadTreeNode> Members

        int IComparer<TreeNode>.Compare(TreeNode x, TreeNode y)
        {
            if (x.Tag is DateTime && y.Tag is DateTime)
            {
                return ((DateTime)y.Tag).CompareTo(((DateTime)x.Tag));
            }

            if (x.Tag is IProject && y.Tag is IProject)
            {
                return ((IProject)x.Tag).Name.CompareTo(((IProject)y.Tag).Name);
            }

            if (x.Tag is ISession && y.Tag is ISession)
            {
                return ((ISession)y.Tag).StartTime.CompareTo(((ISession)x.Tag).StartTime);
            }

            if (x.Tag is INote && y.Tag is INote)
            {
                return ((INote)y.Tag).Time.CompareTo(((INote)x.Tag).Time);
            }

            return 0;
        }

        #endregion
        
        int System.Collections.IComparer.Compare(object x, object y)
        {
            if (x is TreeNode && y is TreeNode)
            {
                return ((IComparer<TreeNode>)this).Compare((TreeNode)x, (TreeNode)y);
            }

            return default(int);
        }
    }
}
